package com.mine.distinctionteam.ui.businessman;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshScrollView;
import com.mine.distinctionteam.R;
import com.mine.distinctionteam.bean.businessman.BusinessmanBean;
import com.mine.distinctionteam.bean.businessman.GoodsListBeans;
import com.mine.distinctionteam.config.UnUsedUrlConfig;
import com.mine.distinctionteam.ui.businessman.adapter.BusinessmanGridAdapter;
import com.mine.distinctionteam.ui.good.GoodsActivity;
import com.mine.distinctionteam.widget.CircleTransform;
import com.mine.distinctionteam.widget.MyGirdView;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class BusinessmanActivity extends AppCompatActivity implements BusinessmanContract.View{
    private Context context=this;
    private PullToRefreshScrollView scrollView;
    private ImageView imageViewHeader;
    private TextView name;
    private TextView count;
    private MyGirdView girdView;
    private BusinessmanPresenter presenter;
    private BusinessmanGridAdapter adapter;
    private List<GoodsListBeans> list=new ArrayList<>();
    private int userid;
    private int page=1;
    private boolean isClear=true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Intent intent=getIntent();
        userid=intent.getIntExtra("userid",-1);
        setContentView(R.layout.activity_businessman);
        scrollView= (PullToRefreshScrollView) findViewById(R.id.scrollView_businessman);
        imageViewHeader= (ImageView) findViewById(R.id.image_businessman_header);
        name= (TextView) findViewById(R.id.textview_businessman_name);
        count= (TextView) findViewById(R.id.textview_businessman_count);
        girdView= (MyGirdView) findViewById(R.id.gridview_businessman);
        adapter=new BusinessmanGridAdapter(context,list);
        girdView.setAdapter(adapter);
        girdView.setFocusable(false);
        girdView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent=new Intent(context, GoodsActivity.class);
                intent.putExtra("id",""+list.get(position).getId());
                startActivity(intent);
            }
        });

        scrollView.setMode(PullToRefreshBase.Mode.BOTH);
        scrollView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ScrollView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ScrollView> refreshView) {
                isClear=true;
                page=1;
                Map<String,String> map=new HashMap<>();
                map.put(UnUsedUrlConfig.Key.USEDID,""+userid);
                map.put(UnUsedUrlConfig.Key.PAGE,""+page);
                presenter.getBusinessman(map);
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ScrollView> refreshView) {
                isClear=false;
                page++;
                Map<String,String> map=new HashMap<>();
                map.put(UnUsedUrlConfig.Key.USEDID,""+userid);
                map.put(UnUsedUrlConfig.Key.PAGE,""+page);
                presenter.getBusinessman(map);
            }
        });
        presenter =new BusinessmanPresenter(this);
        if (userid!=-1){
            Map<String,String> map=new HashMap<>();
            map.put(UnUsedUrlConfig.Key.USEDID,""+userid);
            map.put(UnUsedUrlConfig.Key.PAGE,""+page);
            presenter.getBusinessman(map);
        }
    }

    @Override
    public void getBusinessmanSucess(BusinessmanBean bean) {
        if (bean!=null){
            if (bean.getUser()!=null){
                name.setText(bean.getUser().getNickname());
                count.setText("共发布了"+bean.getUser().getPublishCount()+"件物品，卖出"+bean.getUser().getSoldCount()+"件");
                Glide.with(context).load(bean.getUser().getHeadimg()).transform(new CircleTransform(context)).into(imageViewHeader);
            }
            adapter.reload(bean.getGoodsList(),isClear);
        }
    }
    @Override
    public void getBusinessmanFail(String erro) {

    }


}
